<?php

namespace App\Http\Models;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * request验证
 * @author Jjinfeihu
 * 
 */
abstract class RequestValidator extends FormRequest {

    use \App\Common\Traits\ResponseTrait;

    /**
     * 重写父类验证
     *  
     */
    protected function failedValidation(Validator $validator) {
        $error = $validator->errors()->all();
        throw new HttpResponseException($this->fail(400, $error));
    }

    /**
     * 返回异常
     * 
     */
    protected function fail(int $code, array $errors) {
        return $this->errors(EMPTY_DATA, reset($errors), $code);
    }

}
